package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by Boller on 2022/2/26 17:33
 */

@SuppressWarnings("All")
@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class AdvposController {

    @Autowired
    private AdvposService advposService;
    @Autowired
    private AdvimgService advimgService;

    /**
     * 广告位管理--分页查询
     *
     * @param page
     * @param limit
     * @param advposname
     * @param advposdesc
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Integer page, Integer limit, String advposname, String advposdesc) {
        Page<Advpos> pageInfo = new Page<>(page, limit);

        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(advposname != null, Advpos::getAdvposname, advposname);
        //queryWrapper.like(advposdesc != null, Advpos::getAdvposdesc, advposdesc);
        advposService.page(pageInfo, queryWrapper);

        Page<AdvposDto> advposDtoPage = new Page<>(page, limit);

        List<AdvposDto> AdvposDtos = pageInfo.getRecords().stream().map((advpos) -> {

            Integer id = advpos.getId();
            LambdaQueryWrapper<Advimg> advimgQueryWrapper = new LambdaQueryWrapper<>();
            advimgQueryWrapper.eq(Advimg::getAdvimgpos, id);
            List<Advimg> advimgList = advimgService.list(advimgQueryWrapper);

            AdvposDto advposDto = new AdvposDto();
            BeanUtils.copyProperties(advpos, advposDto);

            advposDto.setAdvposnum(advimgList.size());

            return advposDto;
        }).collect(Collectors.toList());
        advposDtoPage.setRecords(AdvposDtos);
        BeanUtils.copyProperties(pageInfo, advposDtoPage, "records");

        return ResultData.ok(PageResult.init(advposDtoPage));
    }

    /**
     * 广告位管理--查询全部
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        return ResultData.ok(advposService.list());
    }

    /**
     * 广告位管理--根据id查询单个数据
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        return ResultData.ok(advposService.getById(id));
    }

    /**
     * 广告位管理--根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {

        advposService.removeById(id);
        return ResultData.ok("删除成功");
    }

    /**
     * 广告位管理--修改
     *
     * @param advimg
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Advpos advpos) {

        advposService.updateById(advpos);
        return ResultData.ok("修改成功");
    }

    /**
     * 广告位管理--添加
     *
     * @param advimg
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Advpos advpos) {
        advposService.save(advpos);
        return ResultData.ok("添加成功");
    }
}
